package com.csx.saas.platform.system.controller;

import com.csx.basic.annotation.log.SysLog;
import com.csx.basic.base.R;
import com.csx.basic.base.controller.SuperCacheController;
import com.csx.basic.database.mybatis.conditions.Wraps;
import com.csx.basic.utils.TreeUtil;
import com.csx.saas.platform.system.dto.SystemMenuPageQuery;
import com.csx.saas.platform.system.dto.SystemMenuSaveDTO;
import com.csx.saas.platform.system.dto.SystemMenuUpdateDTO;
import com.csx.saas.platform.system.entity.SystemMenu;
import com.csx.saas.platform.system.service.SystemMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 菜单
 * </p>
 *
 * @author ChenSongXin
 * @date 2021-02-28
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/systemMenu")
@Api(value = "SystemMenu", tags = "菜单")
//@PreAuth(replace = "platform:systemMenu:")
public class SystemMenuController extends SuperCacheController<SystemMenuService, Long, SystemMenu, SystemMenuPageQuery, SystemMenuSaveDTO, SystemMenuUpdateDTO> {

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<SystemMenu> systemMenuList = list.stream().map((map) -> {
            SystemMenu systemMenu = SystemMenu.builder().build();
            //TODO 请在这里完成转换
            return systemMenu;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(systemMenuList));
    }


    @ApiOperation(value = "查询系统所有的菜单树", notes = "查询系统所有的菜单树")
    @GetMapping("/tree")
    @SysLog("查询系统所有的菜单树")
    public R<List<SystemMenu>> allTree() {
        List<SystemMenu> list = baseService.list(Wraps.<SystemMenu>lbQ().orderByAsc(SystemMenu::getSortValue));
        return success(TreeUtil.buildTree(list));
    }
}
